package org.rc.dao;

import com.googlecode.objectify.Query;
import java.util.List;
import org.rc.model.Race;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *
 * @author dokeeffe
 */
public class RaceDao extends GenericDao<Race> {

    private static final Logger LOGGER = LoggerFactory.getLogger(RaceDao.class);
    
    
    /**
     * Constructor
     */
    public RaceDao() {
        super(Race.class);
    }


    /**
     * Get a page of races
     * @param start
     * @param pagesize
     * @return
     */
    public List<Race> getPageOfRaces(Integer start, Integer pagesize) {
        Query q = ofy().query(Race.class);
        q.order("-date");
        q.offset(start);
        q.limit(pagesize);
        return q.list();
    }

    /**
     * Get all races for a distance passed.
     * Reads through a generic cache
     * @param meters
     * @return 
     */
    public List<Race> getRacesForDistance(double meters) {
        List<Race> races = (List<Race>) genericCache.get("racesForDistance"+meters);
        if(races==null) {
            Query<Race> q = ofy().query(Race.class);
            q.filter("distanceM", meters);
            q.order("date");
            races = asList(q);
            genericCache.put("racesForDistance"+meters, races);
        }
        LOGGER.debug("located races for milse count: " + races.size() );
        return races;
    }
}
